
{include file="public/top" /}

<body class="layui-anim layui-anim-up">

<div class="x-body">
    <div class="layui-row clear Messagesa">
        <p class="fl">>留言板</p>
    </div>
    <div class="layui-row" style="margin: 10px 0 -10px 0">
        <form class="layui-form layui-col-md12 x-so">
            <input style="width: 200px" type="text" name="name"  placeholder="请输入名称" autocomplete="off" class="layui-input">
            <div class="layui-input-inline">
                <select id="type" name="type" class="valid">
                    <option value="0">请选择是否审核</option>
                    <option value="1">未审核</option>
                    <option value="2">已审核</option>
                </select>
            </div>
            <button class="layui-btn" type="button"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
            <!--分开-->
            <button class="layui-btn layui-btn-danger fr" id="del" type="button"><i class="layui-icon ">&#xe640;</i>批量删除</button>
            <button class="layui-btn layui-btn-warm  fr" id="refresh" type="button"><i class="layui-icon ">&#xe63f;</i>刷新</button>
        </form>
    </div>
    <table class="" lay-filter="test" id="table" >
    </table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">审核</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="reply">回复</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
</div>
<script>
    //接口地址
    //获取列表接口
    var listurl = '/admin/Message/getList';
    //回复页面接口
    var replyurl = "/admin/Message/reply";

    //删除接口
    var delurl = '/admin/Message/del';
    //批量删除接口
    var delAllUrl = '/admin/Message/delAll';
    //获取一条数据接口
    var getOneurl = '/admin/Message/getOne/id/';
    //审核接口
    var auditurl = '/admin/Message/audit';
    layui.use('table', function(){
        var table = layui.table;
        //初始化表格
        table.render({
            elem: '#table'
            ,height: 'full-140'
            ,url: listurl //数据接口
            ,Message: true //开启分页
            ,limit:20
            ,limits:[20,100,300]
            ,id:'idTest'
            ,cols: [[ //表头
                {field: 'id',width:40,title: 'ID', fixed: 'left'}
                ,{type: 'checkbox'}
                ,{field: 'blog_id', title: '博客名称'}
                ,{field: 'name', title: '名称'}
                ,{field: 'phone', title: '电话'}
                ,{field: 'email', title: '邮箱'}
                ,{field: 'website', title: 'website'}
                ,{field: 'content', title: '留言内容'}
                ,{field: 'status', title: '状态'}
                ,{field: 'admin_reply', title: '回复内容'}
                ,{field: 'create_time', title: '评论时间'}
                ,{fixed: 'right',width:150, align:'center', toolbar: '#barDemo'}
            ]]
        });
        //监听表格复选框选择
        //监听工具条
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //监听删除
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: delurl,
                        data: {'id':data.id},
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                layer.msg(success.msg);
                                obj.del();
                                layer.close(index);
                            }else {
                                layer.msg(success.msg)
                            }

                        }
                    });

                });
            }
            //回复
            if(obj.event === 'reply'){
                layer.open({
                    type: 2,
                    area:  ["1200px","800px"],
                    fix: false, //不固定
                    maxmin: true,
                    shade:0.4,
                    title: '回复',
                    content: replyurl+'/id/'+data.id,
                    end: function(){
                        table.reload('idTest', {
                            url: listurl
                            ,where: {'name':0}  //设定异步数据接口的额外参数
                            //,height: 300
                        });

                    }
                });
            }
            //监听审核
            if(obj.event === 'edit'){
                layer.confirm('确定审核通过吗？', function(index){
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: auditurl,
                        data: {'id':data.id},
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                layer.msg(success.msg);
                                layer.close(index);
                            }else {
                                layer.msg(success.msg)
                            }

                        }
                    });

                });
            }
        });

        //批量删除
        $('#del').click(function () {
            var checkStatus = table.checkStatus('idTest');
            if(checkStatus.data.length===0){
                layer.msg("请选择要删除的行");
                return false;
            }
            layer.confirm('真的删除行么', function(index){
                var data1 = checkStatus.data;
                var data2 = [];
                for (var i=0;i<data1.length;i++)
                {
                    data2[i] = data1[i].id;
                }
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: delAllUrl,
                    data: {data:data2},
                    success: function (res) {
                        var success = JSON.parse(res);
                        if(success.code==200){
                            layer.msg(success.msg);
                            table.reload('idTest', {
                                url: listurl
                                ,where: {'name':0}  //设定异步数据接口的额外参数
                                //,height: 300
                            });
                        }else {
                            layer.msg(success.msg)
                        }

                    }
                });
            });
        });
        //刷新表格
        $('#refresh').click(function () {
            table.reload('idTest', {
                url: listurl
                ,where: {'name':0}  //设定异步数据接口的额外参数
                //,height: 300
            });
        });
        //监听搜索提交
        var form = layui.form,layer = layui.layer;
        form.on('submit(sreach)', function(data){
            table.reload('idTest', {
                url: listurl
                ,where: data.field //设定异步数据接口的额外参数
                //,height: 300
            });
        });


    });




</script>

</body>

</html>
